﻿using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices;
using Sliver.Roadnet.RawInfo;
using UnityEngine;

namespace Sliver.Roadnet.Components
{
    // 逐交叉口的数据
    [StructLayout(LayoutKind.Sequential)]
    public struct GBIntersectionEvaluationParameter
    {
        // 绿灯间隔清空率
        public float GreenIntervalClearanceRate;
        // 行人过街保障率
        public float CrossingSupportRate;
        // 溢流率
        public float OverflowRate;
        // 交叉口停车次数
        public int StopCount;
        // 绿灯利用率
        public float GreenUtilizationRatio;
        // 平均控制延误（秒）
        public float AverageControlDelay;
    };

    [StructLayout(LayoutKind.Sequential)]
    public struct RawIntersectionInfo
    {
        public int ID;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
        public string Name;
        public int PhaseIndex;
        public GBIntersectionEvaluationParameter EvalParams;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct RawDrivableInfo
    {
        public int id;
        // [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
        // public string Name;
        public int r_c;
        public int w_c;
    }

    public class IntersectionEntity : MonoBehaviour
    {
        public IntersectionJsonData JsonData { get; protected set; }

        public int Index { get; protected set; }

        protected List<NavigateArrow> NavigateArrowList = new List<NavigateArrow>();
        protected Dictionary<string, NavigateArrow> NavigateArrowMap = new Dictionary<string, NavigateArrow>();

        public TrafficLightController TrafficLightController;

        public void Setup(RoadnetEntity roadnet, IntersectionJsonData jsonData, int index, Transform parent)
        {
            JsonData = jsonData;
            Index = index;
            transform.SetParent(parent);
            gameObject.name = jsonData.id;

            foreach (RawRoadLinkInfo roadLink in jsonData.roadLinks)
            {
                RoadLinkEntity entity = Instantiate(RoadnetEntity.RoadLinkPrototype, transform);
                entity.Setup(roadLink);
            }

            // Debug.Log($"{jsonData.roadLinks.Count} --- {jsonData.trafficLight.roadLinkIndices.Count}");
            // foreach (int i in jsonData.trafficLight.roadLinkIndices)
            // {
            //     Debug.Log(i);
            // }
        }

        public Transform LightPoints;

        public void GenerateLight(RoadnetEntity roadnet, IntersectionJsonData jsonData)
        {
            TrafficLightController = new TrafficLightController(this, jsonData);

            // foreach (string roadName in jsonData.roads)
            // {
            //     Debug.Log(roadName);
            //     if (!roadnet.RoadEntityMap.ContainsKey(roadName))
            //     {
            //         Debug.LogWarning($"road with name {roadName} dose not exists!");
            //         continue;
            //     }
            //     RawRoadInfo road = roadnet.RoadInfoMap[roadName];

                // PhaseLight phaseLight = Instantiate(RoadnetEntity.PhaseLightPrototype, transform);
                // phaseLight.transform.position = road.points[0];
                // PhaseLightList.Add(phaseLight);
            // }
        }

        public void ChangeLightPhase(int phase)
        {
            TrafficLightController.SetPhase(phase);
        }

        public void ShowLaneLinks()
        {
            // foreach (RawRoadLinkInfo roadLink in RawInfo.roadLinks)
            // {
            //     foreach (RawLanelinkInfo laneLink in roadLink.laneLinks)
            //     {
            //         NavigateArrow navigate = GameObject.Instantiate(RoadnetEntity.NavigateArrowPrefab).GetComponent<NavigateArrow>();
            //         // navigate.Setup(Navigators);
            //         navigate.DrawPath(laneLink.points);
            //
            //         // NavigateArrowMap.Add();
            //         NavigateArrowList.Add(navigate);
            //     }
            // }
        }

        public void SetTrafficLight()
        {

        }
    }
}
